package com.automannn.dubbo.genericRpc;

import com.automannn.dubbo.api.HelloService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author automannn
 * @Date 2022/4/20
 * @Description 泛化调用
 */
public class ApiGenericConsumer {
    public static void main(String[] args) {
        ReferenceConfig referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(new ApplicationConfig("my-generic-consumer"));

        RegistryConfig registry1 = new RegistryConfig();
        registry1.setAddress("zookeeper://localhost:2181");
        registry1.setGroup("practice");
        List<RegistryConfig> registries = new ArrayList<>();
        registries.add(registry1);

        referenceConfig.setRegistries(registries);

        //xxx: 泛化调用，消费端可以不依赖api接口
        referenceConfig.setInterface("com.automannn.dubbo.api.HelloService");
        referenceConfig.setVersion("1.0");
        referenceConfig.setGroup("practice");
        //xxx: 设置为泛化调用
        referenceConfig.setGeneric("true");

        GenericService genericService = (GenericService) referenceConfig.get();
        genericService.$invoke("sayHello",new String[]{"java.lang.String"},new Object[]{"generic Call"});

        HashMap<String,Object> param = new HashMap<>();
        param.put("id","1");
        param.put("name","automannn");
        String result = (String) genericService.$invoke("invokeBean",new String[]{"com.automannn.dubbo.bean.MyDTO"},new Object[]{param});
        System.out.println(result);
    }
}
